define(['jquery',
        'underscore',
        'handlebar',
        'backbone',
        'views/Vcarousel',
        'views/detailsProductInfo/VdetailsProductInfo',
       // '../../collection/product/Cproduct',
        '../../collection/product/CsearchProductByCategory',
        'text!../../../templates/common/products.html'
        ],
function ($,
         Underscore,
         Handlebar,
         Backbone,
         Vcarousel,
         VdetailsProductInfo,
         //CollectoionProdcuts,
         CollectionSearchProductByCategory,
         ProductsTemplate) {

     window.VItemsRow = Backbone.View.extend({
        el: $('section#items-row'),
        initialize: function () {
           
            //console.log('views/product/VProducts')
        },
        events:{
            'click a.product-detailer-view': 'loadProductDetailsView',           
        },               
        displayInitProducts: function (catId) {
          
        },
        render: function (collectoionProdcuts) {            
            if (collectoionProdcuts.toJSON().length != 0) {
                var productsTemplate = Handlebars.compile(ProductsTemplate);
                var productsTemplateDOM = productsTemplate(collectoionProdcuts.toJSON());
                this.$el.html(productsTemplateDOM);
            }
            else {
                /*$('section#items-row section.row').html('')
                 $('#messageBlock').removeClass('hide').addClass('show');
                 $('#alert-box').html("Your search <strong>" + vsearchItem +"</strong> did not match any products, Please try again")*/
            }
            

        },      
        refreshProducts: function (catId) {
            var that = this;
            var collectoionByCatProdcuts = new CollectoionProdcuts(catId);
            collectoionByCatProdcuts.fetch({
                success: function (collectoionByCatProdcuts) {
                    that.render(collectoionByCatProdcuts);
                },
                error: function (){
                    console.log('Error in fetching Category Items');
                }
            });
        },

        searchByItem: function (placeId,itemName) {
            
            window.placeId = placeId;
            window.itemName = itemName;
            //console.log('searchByItem', placeId, itemName);
            //updating browser current URL
            var currentURL = window.location.href;
            currentURL = '#search?item/'+itemName;
            window.location.href = currentURL;
            
            var that = this;
            var collectionSearchProductByCategory = new CollectionSearchProductByCategory(placeId, itemName);
            collectionSearchProductByCategory.fetch({
                success: function (collectionSearchProductByCategory) {                  
                    //hiding the carosel
                    var vcarousel = new Vcarousel();
                    vcarousel.hideCarosel();
                    that.render(collectionSearchProductByCategory);
                },
                error: function (err) {                 
                    console.log("%c ERROR: in searchByItem while fetching Products","color: red; font-size:small",err);
                }
            });
        },
        loadProductDetailsView: function (e) {
           
            //hide filter;       
            var appView = new AppView();
            appView.hideLeftPanel();
            //for global access            
            this.removeSerachProductView(e.currentTarget.id.split('-')[1]);
        },
        removeSerachProductView: function (prodId) {           
            $(this.el).empty();
            var vdetailsProductInfo = new VdetailsProductInfo();
            vdetailsProductInfo.fetchDetailedProductInfo(prodId);
        }        
    });
    return VItemsRow;
});